﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace AMM.CustomWinControls {
    public partial class TransparencyLabelEx : UserControl {
        public TransparencyLabelEx() {
            InitializeComponent();
            // Add any initialization after the InitializeComponent() call.
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.Opaque, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.components = new Container();
            RF = new RectangleF(0, 0, base.Width, base.Height);
            LabelForeColorBrush = new SolidBrush(base.ForeColor);
        }

        private void TransparentLabel_Resize(object sender, System.EventArgs e) {
            ResizeControl();
        }

        #region Private

        private StringFormat sFormat;
        private RectangleF RF;
        SolidBrush LabelForeColorBrush = null;

        #endregion

        #region Subroutines

        /// <summary>
        /// Updates Text of this control.
        /// </summary>
        /// <remarks></remarks>
        private void UpdateText() {
            try {
                sFormat = new StringFormat();
                var _with1 = sFormat;
                switch (TextAlignment) {
                    case ContentAlignment.BottomCenter:
                        sFormat.Alignment = StringAlignment.Center;
                        sFormat.LineAlignment = StringAlignment.Far;
                        break;
                    case ContentAlignment.BottomLeft:
                        sFormat.Alignment = StringAlignment.Near;
                        sFormat.LineAlignment = StringAlignment.Far;
                        break;
                    case ContentAlignment.BottomRight:
                        sFormat.Alignment = StringAlignment.Far;
                        sFormat.LineAlignment = StringAlignment.Far;
                        break;
                    case ContentAlignment.MiddleLeft:
                        sFormat.Alignment = StringAlignment.Near;
                        sFormat.LineAlignment = StringAlignment.Center;
                        break;
                    case ContentAlignment.MiddleCenter:
                        sFormat.Alignment = StringAlignment.Center;
                        sFormat.LineAlignment = StringAlignment.Center;
                        break;
                    case ContentAlignment.MiddleRight:
                        sFormat.Alignment = StringAlignment.Far;
                        sFormat.LineAlignment = StringAlignment.Center;
                        break;
                    case ContentAlignment.TopCenter:
                        sFormat.Alignment = StringAlignment.Center;
                        sFormat.LineAlignment = StringAlignment.Near;
                        break;
                    case ContentAlignment.TopLeft:
                        sFormat.Alignment = StringAlignment.Near;
                        sFormat.LineAlignment = StringAlignment.Near;
                        break;
                    case ContentAlignment.TopRight:
                        sFormat.Alignment = StringAlignment.Far;
                        sFormat.LineAlignment = StringAlignment.Near;
                        break;
                }
                _with1.FormatFlags = StringDirection;
                ResizeControl();

            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// Resizes Transparent Label Control.
        /// </summary>
        /// <remarks></remarks>
        private void ResizeControl() {
            RF.Size = this.Size;
            Invalidate();
        }

        #endregion

        #region Properties

        StringFormatFlags _StringDirection = StringFormatFlags.NoClip;
        [Description("The Direction of the Text."), DefaultValue(StringFormatFlags.NoClip)]
        public StringFormatFlags StringDirection {
            get { return _StringDirection; }
            set {
                _StringDirection = value;
                UpdateText();
            }
        }

        private System.Drawing.ContentAlignment _TextAlignment = ContentAlignment.MiddleCenter;
        [Description("The Text Alignment that will appear on this control."), DefaultValue(ContentAlignment.MiddleCenter)]
        public System.Drawing.ContentAlignment TextAlignment {
            get { return _TextAlignment; }
            set {
                _TextAlignment = value;
                UpdateText();
            }
        }

        #endregion

        #region Overrides

        public override System.Drawing.Color ForeColor {
            get { return base.ForeColor; }
            set {
                base.ForeColor = value;
                LabelForeColorBrush = new SolidBrush(value);
            }
        }
        /// <summary>
        /// The text to be displayed in supports with real transparency.
        /// </summary> 
        /// <remarks></remarks>
        string _Labeltext = "TransparentLabel";
        [Description("The text to be displayed in supports with real transparency."), Category("Text"), DefaultValue("TransparentLabel")]
        public string LabelText {
            get { return _Labeltext; }
            set {
                _Labeltext = value;
                Invalidate();
            }
        }

        [Browsable(false)]
        public override System.Drawing.Color BackColor {
            get { return base.BackColor; }
            set { base.BackColor = value; }
        }
        protected override CreateParams CreateParams {
            get {
                CreateParams cp = base.CreateParams;
                cp.ExStyle = cp.ExStyle | 0x20;
                return cp;
            }
        }
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {
            try {
                base.OnPaint(e);
                // draw the text on the control
                e.Graphics.DrawString(LabelText, base.Font, LabelForeColorBrush, RF, sFormat);
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion
    }
}
